home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / pyshared / PIL / ImageDraw2.py < prev    next >
Text File  |  2006-12-03  |  3KB  |  109 lines

  1. #
  2. # The Python Imaging Library
  3. # $Id: ImageDraw.py 2134 2004-10-06 08:55:20Z fredrik $
  4. #
  5. # WCK-style drawing interface operations
  6. #
  7. # History:
  8. # 2003-12-07 fl   created
  9. # 2005-05-15 fl   updated; added to PIL as ImageDraw2
  10. # 2005-05-15 fl   added text support
  11. # 2005-05-20 fl   added arc/chord/pieslice support
  12. #
  13. # Copyright (c) 2003-2005 by Secret Labs AB
  14. # Copyright (c) 2003-2005 by Fredrik Lundh
  15. #
  16. # See the README file for information on usage and redistribution.
  17. #
  18.  
  19. import Image, ImageColor, ImageDraw, ImageFont, ImagePath
  20.  
  21. class Pen:
  22.     def __init__(self, color, width=1, opacity=255):
  23.         self.color = ImageColor.getrgb(color)
  24.         self.width = width
  25.  
  26. class Brush:
  27.     def __init__(self, color, opacity=255):
  28.         self.color = ImageColor.getrgb(color)
  29.  
  30. class Font:
  31.     def __init__(self, color, file, size=12):
  32.         # FIXME: add support for bitmap fonts
  33.         self.color = ImageColor.getrgb(color)
  34.         self.font = ImageFont.truetype(file, size)
  35.  
  36. class Draw:
  37.  
  38.     def __init__(self, image, size=None, color=None):
  39.         if not hasattr(image, "im"):
  40.             image = Image.new(image, size, color)
  41.         self.draw = ImageDraw.Draw(image)
  42.         self.image = image
  43.         self.transform = None
  44.  
  45.     def flush(self):
  46.         return self.image
  47.  
  48.     def render(self, op, xy, pen, brush=None):
  49.         # handle color arguments
  50.         outline = fill = None; width = 1
  51.         if isinstance(pen, Pen):
  52.             outline = pen.color
  53.             width = pen.width
  54.         elif isinstance(brush, Pen):
  55.             outline = brush.color
  56.             width = brush.width
  57.         if isinstance(brush, Brush):
  58.             fill = brush.color
  59.         elif isinstance(pen, Brush):
  60.             fill = pen.color
  61.         # handle transformation
  62.         if self.transform:
  63.             xy = ImagePath.Path(xy)
  64.             xy.transform(self.transform)
  65.         # render the item
  66.         if op == "line":
  67.             self.draw.line(xy, fill=outline, width=width)
  68.         else:
  69.             getattr(self.draw, op)(xy, fill=fill, outline=outline)
  70.  
  71.     def settransform(self, (xoffset, yoffset)):
  72.         self.transform = (1, 0, xoffset, 0, 1, yoffset)
  73.  
  74.     def arc(self, xy, start, end, *options):
  75.         self.render("arc", xy, start, end, *options)
  76.  
  77.     def chord(self, xy, start, end, *options):
  78.         self.render("chord", xy, start, end, *options)
  79.  
  80.     def ellipse(self, xy, *options):
  81.         self.render("ellipse", xy, *options)
  82.  
  83.     def pieslice(self, xy, start, end, *options):
  84.         self.render("pieslice", xy, start, end, *options)
  85.  
  86.     def line(self, xy, *options):
  87.         self.render("line", xy, *options)
  88.  
  89.     def rectangle(self, xy, *options):
  90.         self.render("rectangle", xy, *options)
  91.  
  92.     def ellipse(self, xy, *options):
  93.         self.render("ellipse", xy, *options)
  94.  
  95.     def polygon(self, xy, *options):
  96.         self.render("polygon", xy, *options)
  97.  
  98.     def symbol(self, xy, symbol, *options):
  99.         raise NotImplementedError("not in this version")
  100.  
  101.     def text(self, xy, text, font):
  102.         if self.transform:
  103.             xy = ImagePath.Path(xy)
  104.             xy.transform(self.transform)
  105.         self.draw.text(xy, text, font=font.font, fill=font.color)
  106.  
  107.     def textsize(self, text, font):
  108.         return self.draw.textsize(text, font=font.font)
  109.